//
//  TableViewController.swift
//  Swift4.0_Learn
//
//  Created by lantaiyuan on 2017/9/29.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import UIKit
let cellID = "PrototypeCell"

class TableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    /// 懒加载
    lazy var tableView: UITableView = {
        let tableView = UITableView.init(frame: CGRect(x:0,y:64,width:UIScreen.main.bounds.width,height:UIScreen.main.bounds.height - 64), style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    //数据源
    var restaurantNames = ["cg1","cg2","cg3","cg4","cg5","cg6","cg7","cg8","cg9","cg10","cg11"]
    //定义一个布尔类型的全局数组restIsMask，用来表示每一行的标记情况
    var restIsMark = [Bool](repeating: false, count: 11)
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //self.navigationController?.navigationBar.tintColor = UIColor.orange
        //自定义NavBar
        self.navigationController?.navigationBar.backgroundColor = UIColor.brown
//        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        //消除阴影
        navigationController?.navigationBar.shadowImage = UIImage()
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.white
        self.navigationItem.title = "TableView"
        view.addSubview(tableView)
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        tableView.register(UINib.init(nibName: cellID, bundle: nil), forCellReuseIdentifier: cellID)
        //不使用xib
        tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")
    }
    // MARK -  UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return restaurantNames.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
        if cell.isEqual(nil) {
            cell = CustomTableViewCell.init(style: .default, reuseIdentifier: "CustomTableViewCell")
        }
        let restName = restaurantNames[indexPath.row]
        let restLocation = "Hello"
        let imageName = "icon_head_portrait"
        let restType = "World"
        cell.initWith(thubnailImage: imageName, restName: restName, restLocation: restLocation, restType: restType)
        if restIsMark[indexPath.row] {
            cell.accessoryType = .checkmark
        }else{
            cell.accessoryType = .none
        }
//        cell?.initWith(imageName, restName: restName, restLocation: restLocation, restType: restType)
//        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! PrototypeCell
//        cell.nameLabel.text = restaurantNames[indexPath.row]
//        cell.subTitleLbl.text = "Hello! \(indexPath.row)"
//        cell.iconImage.image = UIImage.init(named: "icon_head_portrait")
//        cell.selectionStyle = .none
//        cell.accessoryType = .checkmark
        return cell
    }
    // MARK -UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.00001
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.0001
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       
        tableView.deselectRow(at: indexPath, animated: true)
        
        let callActionHandle = {(action:UIAlertAction!) -> Void in
            let alertMessage = UIAlertController.init(title: "Service is unavalable", message: "You can choice another rest", preferredStyle: .alert)
            alertMessage.addAction(UIAlertAction.init(title: "ok", style: .default, handler: nil))
            self.present(alertMessage, animated: true, completion: nil)
        }
        
        let option = UIAlertController.init(title: nil, message: "What are you goning to do?", preferredStyle: .actionSheet)
        let callAction = UIAlertAction.init(title: "Call"+"180-123-\(indexPath.row)", style: .default, handler: callActionHandle)//自定义的callActionHandler来响应点击的事件
        let cancelAction = UIAlertAction.init(title: "ok", style: .cancel, handler: nil)
        let markAction = UIAlertAction.init(title: "I'm here", style: .default) {[weak self] (action) in
            let cell = tableView.cellForRow(at: indexPath)//获取当前的cell
            cell?.accessoryType = .checkmark//对号
            self?.restIsMark[indexPath.row] = true
//            let vc = StructViewController()!
//            
//            self?.navigationController?.pushViewController(vc!, animated: true)

        }
        option.addAction(markAction)
        option.addAction(callAction)
        option.addAction(cancelAction)
        self.present(option, animated: true, completion: nil)
        
        
    }
    //swift系统方法删除cell
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {//删除
            self.restaurantNames.remove(at: indexPath.row)
            self.restIsMark.remove(at: indexPath.row)
        }
        self.tableView.deleteRows(at: [indexPath], with: .left)
    }
    //
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let shareAction = UITableViewRowAction.init(style: .default, title: "Share") { (action, indexpath) in
            //逻辑处理
            let menu = UIAlertController.init(title: "Share Action", message: nil, preferredStyle: .actionSheet)
            let cancel = UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil)
            menu.addAction(cancel)
            self.present(menu, animated: true, completion: nil)
        }
        let deleteAction = UITableViewRowAction.init(style: .default, title: "Delete") { (action, indexpath) in
            //逻辑处理
            self.restaurantNames.remove(at: indexPath.row)
            self.restIsMark.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath], with: .left)
        }
       return [shareAction,deleteAction]
    }
//    override var prefersStatusBarHidden: Bool{////隐藏上边栏中的电量、信号等信息
//        return true
//    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
